<?php
//我的任务
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['set']=$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$return['params']=array('status'=>0);
	set_html('title','我的任务');
	$return['list']=array();
	$return['status']=$status;
	$this->model('model')->uprecord();
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
	);
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}else{
		$params['status']=0;
		$return['params']['status']=0;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$return['list']=pdo_getslice('jy_inclock_task_record',$params,array($pindex,$psize),$total,array(),'','id desc');
	foreach($return['list'] as $k=>&$item){
		if($item['task_progress']>=$item['task_demand']){
			$item['ispass']=1;
			if(!$item['finishtime']){
				$this->update('task_record',array('finishtime'=>TIMESTAMP),array('id'=>$item['id']));
				$item['finishtime']=TIMESTAMP;
			}
		}
		$isget=$this->get('task_reward',array('recordid'=>intval($item['id']),'mid'=>intval($item['mid']),'uniacid'=>$_SESSION['uniacid']));
		if($isget['id']){
			$item['isreward']=1;	
		}
		$task=$this->pdo_get('task_list',array('id'=>$item['taskid'],'uniacid'=>$_SESSION['uniacid']));
		$item['rewarddesc']='奖励';
		$reward=json_decode($task['reward'],true);
		$credit_types=credit_types();
		foreach($reward as $key=>$value){
				if(strpos($key,'credit') !== false && $value>0){
					$type=substr($key, -1);
					$item['rewarddesc'].='['.$value.$credit_types[$type].'] ';
				}
		}
		if($item['finishtime'] && $task['cangettime'] && $item['finishtime']>=TIMESTAMP-($task['cangettime']*24*3600)){
			$item['lastgettime']=$item['finishtime']+($task['cangettime']*24*3600)-TIMESTAMP;
			$item['lastgettime']=$item['lastgettime']/(24*3600);
			$item['lastgettime']=price($item['lastgettime']);
		}
		$item['finishtime']=date('Y-m-d H:i:s',$item['finishtime']);
		if($task['type']==3){
			$isvote=pdo_get('jy_vote_votelog',array('id'=>$task['vote_oid'],'uniacid'=>$_SESSION['uniacid']),'id');
			if($isvote['id']){
				$insert=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$_SESSION['uid'],
						'record_id'=>intval($item['id']),
						'status'=>1,
						'type'=>3,
						'intime'=>TIMESTAMP,
				);
				$this->pdo_insert('task_record_log',$insert);
				//审核通过
				$record_update=array(
					'task_progress +='=>1,
				);
				if($item['task_progress']+1>=$item['task_demand']){
					$record_update['status']=1;
					$record_update['finishtime']=TIMESTAMP;
				}
				$this->update('task_record',$record_update,array('id'=>$item['id']));
			}
		}
	}
	exi($return);
}	
if($_RQ['op']=='checkfinish'){
	$record_id=intval($_RQ['id']);
	if(!$record_id){
		$rewardid=intval($_RQ['rewardid']);
		$record_id=$this->pdo_getcolumn('task_reward',array('id'=>$rewardid,'uniacid'=>$_SESSION['uniacid']),'recordid');
	}
	$this->model('model')->checkfinish($record_id);
	exi('领取奖励成功！','error','/mine/main/');
}
